<?php 
/**
 * Hockey extension for Joomleague
 * @version $Id: controller.php 132 2008-09-18 08:55:06Z julien.vonthron $
 * @package JlgHockeyExtension
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * JlgHockeyExtension is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */
 
include_once('classes/HockeyProject.php');

// change the type of $joomleague to use extended functions
if ( $save_id = $joomleague->id )
{
  $joomleague = new HockeyProject( $database);
  $joomleague->load( $save_id );
  $overall_config=array_merge($overall_config, $joomleague->getTemplateConfig("hockey_extension.tpl"));
}

switch($jl_func){
	//remember to set $jl_func to a dummy value if you don't want to trigger 
	//matching value in default switch
	case 'last3':
		require('models'.'/'.'last3.php');
    showLast3();
    $jl_func = "end";
		break;
		
  case "showplayersranking";
    require('models'.'/'.'playersranking.php');
    showPlayersRanking();
    $jl_func = "end";
    break;
    
    case "showreport";
      $match_id = mosGetParam($_REQUEST,'mid', 0);
      $sql="SELECT summary FROM #__joomleague_matches where match_id=$match_id";
			$joomleague->_db->setQuery($sql);
			if ($res = $joomleague->_db->loadResult())
	    echo "match summary: $res";
      $jl_func = "end";
      break;
    
  case "showplayer":
    // pid is player id not playertool id
    $showPlayer_config=$joomleague->getTemplateConfig("show_player.tpl");
    if($pid > 0) {
      $history = jl_getPlayerHistory($pid, 'DESC');
      $historyStaff = jl_getStaffHistory ($pid, 'DESC');
      
      $query = "SELECT p.*, 
                       pt.id AS playertoolid, pt.*, 
                       pp.name as position, pp.isStaff, 
                       proj.name as proj_name, 
                       t.name as team_name
                FROM #__joomleague_playertool pt
                INNER JOIN #__joomleague_team_joomleague tt ON (pt.team_id = tt.team_id AND tt.project_id=pt.project_id) 
                INNER JOIN #__joomleague_players p ON p.id = pt.player_id
                INNER JOIN #__joomleague_teams AS t ON t.id = pt.team_id
                INNER JOIN #__joomleague_positions pp ON pp.id = pt.position_id
                INNER JOIN #__joomleague AS proj ON pt.project_id = proj.id
                INNER JOIN #__joomleague_seasons AS s ON s.id = proj.season_id
                INNER JOIN #__joomleague_leagues AS l ON l.id = proj.league_id
                WHERE p.id = $pid AND proj.published=1
                ORDER BY s.ordering DESC, l.ordering DESC, proj.name ASC";
      $database->setQuery($query);
      $rows = $database->loadObjectList();
      
      if(count($rows) > 0){
        // find row of current project
        foreach ($rows as $k => $r) {
        	if ($r->project_id == $joomleague->id) {
        		$row=&$rows[$k];
        		break;
        	}
        }
        
        // get contact_id if needed
        if ( $showPlayer_config['show_user_profile'] == 1 ) {
             $query = "SELECT id FROM #__contact_details WHERE user_id = $row->jl_user_id AND catid=".$showPlayer_config['contact_cat_id'];
             $database->setQuery($query);
             if (!$contact_id = $database->loadResult()) {
               //echo $database->getQuery();
             }
        }
        $eventtypes = $joomleague->getPositionEventTypes($row->position_id);
               
        $mainframe->setPageTitle(_JL_PAGE_TITLE_PLAYER.' - '.$row->firstname.' '.$row->lastname);
        require(getTemplateFile(JL_TEMPLATE_PATH, 'show_player.tpl.php'));
      }
      else print "no data";
    }
    $jl_func = "end";
    break;
    
  case "showplayers":
  
    $showPlayers_config=$joomleague->getTemplateConfig("show_players.tpl");
    
    if(is_a($teamtool,'Teamtool')){
      $team = new JL_Team( $database );
      $team->load($teamtool->team_id);
      $mainframe->setPageTitle(_JL_PAGE_TITLE_PLAYERS.' - '.$team->name);
//    $mainframe->appendPathWay(_JL_PAGE_TITLE_PLAYERS.' - '.$team->name);
      #list players of a team
      $query = "SELECT pt.*, p.*, p.id AS pid, pos.name as position
                          FROM #__joomleague_playertool pt
                          INNER JOIN #__joomleague_players p ON pt.player_id = p.id
                          INNER JOIN #__joomleague_positions pos ON pos.id = pt.position_id
                          WHERE pt.team_id = '$team->id' AND
                          pt.project_id = '$teamtool->project_id'
                          ORDER BY pos.ordering, pt.position_id, pt.position_number";
      $database->setQuery($query);
      $rows = $database->loadObjectList();
      //print_r($rows); exit;

      if ($showPlayers_config['show_stats']==1&&count($rows)>0) {
        $PosEventtypes=$joomleague->getPositionEventTypes();
        foreach ($rows AS $row) {
          $playerEvents[$row->id] = array();
          if ( isset($PosEventtypes[$row->position_id]) ) {
            foreach ($PosEventtypes[$row->position_id] as $event) {    
              $playerStats[$row->id][$event->eventtype_id] = $joomleague->getPlayerEvents($row->player_id, $event->eventtype_id, $row->team_id);
            }
          }
        }
        //print_r($playerStats);exit;
      }
      
      $query = sprintf("SELECT p.*, pos.name as position, ts.picture
                          FROM #__joomleague_teamstaff_project ts
                          INNER JOIN #__joomleague_players p ON ts.person_id = p.id
                          INNER JOIN #__joomleague_positions pos ON pos.id = ts.position_id
                          WHERE projectteam_id= '%s'     
                          ORDER BY pos.ordering, ts.position_id
                      ", $teamtool->id);
      $database->setQuery($query);
      $stafflist = $database->loadObjectList();   
      require(getTemplateFile(JL_TEMPLATE_PATH, 'show_players.tpl.php'));
    }
    else print "no data!";   
    $jl_func = "end";
    break; 
}

?>
